import axios from "../../api/request";
const userUpdateForm = document.getElementById('userUpdateForm')
const username = document.querySelector('#username')
const profileInput = document.querySelector('#profile')
const profileImg = document.querySelector('#profileImg')
const userIdInp = document.querySelector('#userId')
const id = window.location.hash.substring(1);
(async function (){
    const {data:{data}} = await axios({
    url:`/admin/user/?id=${id}`,
    })
    // console.log(data);
    userIdInp.value=data._id
    username.value=data.username
    profileImg.src='http://localhost:8081'+data.profile
})();
//解析图片
profileInput.onchange=function(e){
    // 读取文件对象
    let fileReader = new FileReader();
     // 获取文件
    let file = e.target.files[0]
    // 使用base64方式读取文件
    fileReader.readAsDataURL(file)

    fileReader.onload=function(){
        // console.log(fileReader.result);
        
        let profile = fileReader.result
        profileImg.src=profile
    }
}
// console.log(userAddForm);

userUpdateForm.addEventListener('submit',async function(e){
    e.preventDefault()
    const formData = new FormData(userUpdateForm)
    // console.log(formData);
    
    const {data} = await axios({
        method:'put',
        url:'admin/user',
        data:formData
    })
    // console.log(data);
    if(data.code === 2000){
        alert('更新用户成功')
        window.location.href='/users.html'
    }
    
})